package com.test.designPattern.creationType.singletonPattern;

/**
 * 懒汉单例模式
 *
 * @author wangjie
 * @version V1.0
 * @date 2019/12/14
 */
public class SingletonPattern {
    private static volatile SingletonPattern instance=null;

    private SingletonPattern(){
        System.out.println(Thread.currentThread().getName()+"\t 构造方法");
    }

    /**
     * 双重检测机制
     * @return
     */
    public static SingletonPattern getInstance(){
        if(instance==null){
            synchronized (SingletonPattern.class){
                if(instance==null){
                    instance=new SingletonPattern();
                }
            }
        }
        return instance;
    }

    public static void main(String[] args) {
        for (int i = 1; i <=10; i++) {
            new Thread(() ->{
                SingletonPattern.getInstance();
            },String.valueOf(i)).start();
        }
    }
}
